package com.xinchuang.service.targetSystem.Impl.damage;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xinchuang.comment.R;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinchuang.entity.targetSystem.damage.DictionaryTable;
import com.xinchuang.mapper.targetSystem.damage.DictionaryTableMapper;
import com.xinchuang.service.targetSystem.damage.IDictionaryTableService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 数据字典表 服务实现类
 * </p>
 *
 * @author baomidou
 * @since 2024-04-26
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class DictionaryTableServiceImpl extends ServiceImpl<DictionaryTableMapper, DictionaryTable> implements IDictionaryTableService {

    private final DictionaryTableMapper dictionaryTableMapper;

    @Override
    public String getFieldTypeList(JSONObject jsonObject) {

        List<DictionaryTable> dictionaryTableList = getDictionaryTableList("field_type");
        List<Map<String, Object>> list = dictionaryTableList.stream().map(dictionaryTable -> {
            Map<String, Object> map = new HashMap<>();
            map.put("type_id", dictionaryTable.getValue());
            map.put("type_name", dictionaryTable.getLabel());
            return map;
        }).collect(Collectors.toList());

        Map<String, Object> map = new HashMap<>();
        map.put("dataList", list);
        return JSON.toJSONString(R.data(map, "获取成功"));
    }

    @Override
    public String getShowTypeList(JSONObject jsonObject) {
        List<DictionaryTable> dictionaryTableList = getDictionaryTableList("show_type");
        List<Map<String, Object>> list = dictionaryTableList.stream().map(dictionaryTable -> {
            Map<String, Object> map = new HashMap<>();
            map.put("type_id", dictionaryTable.getValue());
            map.put("type_name", dictionaryTable.getLabel());
            return map;
        }).collect(Collectors.toList());

        Map<String, Object> map = new HashMap<>();
        map.put("dataList", list);
        return JSON.toJSONString(R.data(map, "获取成功"));
    }

//    @Override
//    public R addDicInfo(DictionaryTable dictionaryTable) {
//        try {
//            dictionaryTable.setId(UUID.randomUUID().toString().replace("-", ""));
//            boolean num = dictionaryTableMapper.insert(dictionaryTable) > 0;
//            return R.success("添加成功");
//        } catch (Exception e) {
//            e.printStackTrace();
//            return R.fail("添加失败");
//        }
//    }

    @Override
    public List<Map<String, Object>> getDictList(String type) {
        List<DictionaryTable> dictionaryTableList = getDictionaryTableList(type);
        List<Map<String, Object>> list = dictionaryTableList.stream().map(dictionaryTable -> {
            Map<String, Object> map = new HashMap<>();
            map.put("value", dictionaryTable.getValue());
            map.put("label", dictionaryTable.getLabel());
            map.put("type", dictionaryTable.getType());
            return map;
        }).collect(Collectors.toList());
        return list;
    }

    /**
     * 查询字典属
     *
     * @param type
     * @return
     */
    public List<DictionaryTable> getDictionaryTableList(String type) {
        return dictionaryTableMapper.selectList(new QueryWrapper<DictionaryTable>().lambda()
                //只返回需要的字段
                .select(DictionaryTable::getValue,DictionaryTable::getLabel,DictionaryTable::getType)
                .eq(DictionaryTable::getType, type)
                .orderByAsc(DictionaryTable::getCreateTime));
    }

}
